package com.zzptc.lzh.myapp11.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zzptc.lzh.myapp11.R;
import com.zzptc.lzh.myapp11.databinding.RvItemBinding;
import com.zzptc.lzh.myapp11.entity.ResultsBean;

import org.litepal.LitePal;

import java.util.List;

public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.ViewHolder> {
    private final List<ResultsBean>resultsBeans;
    private final Context mContext;

    public MyRvAdapter(Context mContext) {
        resultsBeans= LitePal.findAll(ResultsBean.class);
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item,parent,false);//原来的绑定
        RvItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.rv_item, parent, false);//现在databinding的绑定
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ResultsBean result = resultsBeans.get(position);
        holder.binding.tvTitle.setText(result.getTitle());
        Glide.with(mContext).load(result.getCover()).into(holder.binding.ivCover);
    }

    @Override
    public int getItemCount() {
        return resultsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final RvItemBinding binding;
        public ViewHolder(@NonNull RvItemBinding binding) {
            super(binding.getRoot());
            this.binding=binding;
        }
    }
}
